const express = require('express');
const loginMiddleware = require("../middleware/LoginMiddleware");

const ReadedDao = require('../database/ReadedDao')

const router = express.Router();

router.post("/:noticeID", loginMiddleware, async (req, res) => {
    let user = req.user;
    let notice_id = req.params.noticeID;
    try {
        let result = await ReadedDao.addReaded(notice_id, user.user_id);
        console.log("/Readeds添加的结果/:noticeID是:\n" +JSON.stringify(result) + "\n————\n");
        res.send({ code: 2000, msg: "添加成功", data: null });
    } catch (err) {
        console.log(err);
        res.send({ code: 5000, msg: "添加失败", data: null });
    }
})

router.post("/:noticeID/:userID", loginMiddleware, async (req, res) => {
    let notice_id = req.params.noticeID;
    let user_id = req.params.userID;
    try {
        let result = await ReadedDao.addReaded(notice_id, user_id);
        console.log(`/Readeds添加的结果/:noticeID=${notice_id}/:userID=${user_id}是:\n` +JSON.stringify(result) + "\n————\n");
        res.send({ code: 2000, msg: "添加成功", data: null });
    } catch (err) {
        console.log(err);
        res.send({ code: 5000, msg: "添加失败", data: null });
    }
})

router.delete("/noticeId/:noticeId/userId/:userId",loginMiddleware,async (req,res)=>{
    let noticeId = req.params.noticeId;
    let userId = req.params.userId;
    try{
        let result = await ReadedDao.deleteReadedByNoticeIdAndUserId(noticeId,userId);
        console.log("/Readeds删除的结果/noticeId/:noticeId/userId/:userId是:\n" +JSON.stringify(result) + "\n————\n");
        res.send({code:2000,msg:"删除成功",data:null});
    }catch(err){
        console.log(err);
        res.send({code:5000,msg:"删除失败",data:null});
    }
})

router.delete("/noticeId/:noticeId",loginMiddleware,async (req,res)=>{
    let noticeId = req.params.noticeId;
    try{
        let result = await ReadedDao.deleteReadedByNoticeId(noticeId);
        console.log("/Readeds删除的结果/noticeId/:noticeId是:\n" +JSON.stringify(result) + "\n————\n");
        res.send({code:2000,msg:"删除成功",data:null});
    }catch(err){
        console.log(err);
        res.send({code:5000,msg:"删除失败",data:null});
    }
})

router.delete("/:ReadedID", loginMiddleware, async (req, res) => {
    let { ReadedID } = req.params.ReadedID;
    try {
        let result = await ReadedDao.delReaded(ReadedID);
        console.log("/Readeds删除的结果/:ReadedID是:\n" +JSON.stringify(result) + "\n————\n");
        res.send({ code: 2000, msg: "删除成功", data: null });
    } catch (err) {
        console.log(err);
        res.send({ code: 5000, msg: "删除失败", data: null });
    }
})

router.get("/noticeId/:noticeId", loginMiddleware, async (req, res) => {
    let notice_id = req.params.noticeId;
    try {
        let result = await ReadedDao.selectReadedsByNoticeID(notice_id);
        console.log("/Readeds查询的结果/noticeId/:noticeId是:\n" +JSON.stringify(result) + "\n————\n");
        res.send({ code: 2000, msg: "查询成功", data: result });
    }
    catch (err) {
        console.log(err);
        res.send({ code: 5000, msg: "查询失败", data: null });
    }
})

router.get("/noticeId/:ifReaded([01])/:noticeId", loginMiddleware, async (req, res) => {
    let ifReaded = req.params.ifReaded;
    let notice_id = req.params.noticeId;
    console.log("在/Readeds/noticeId/:ifReaded([01])里的ifReaded是:" +ifReaded + "notice_id是:" +notice_id + "\n————\n");
    try {
        if (ifReaded == '1') {
            let result = await ReadedDao.selectReadedByNoticeID(notice_id);
            console.log("/Readeds查询的结果/noticeId/:ifReaded([1])是:\n" +JSON.stringify(result) + "\n————\n");
            res.send({ code: 2000, msg: "查询成功", data: result });
        } else {
            let result = await ReadedDao.selectUnReadedByNoticeID(notice_id);
            console.log("/Readeds查询的结果/noticeId/:ifReaded([0])是:\n" +JSON.stringify(result) + "\n————\n");
            res.send({ code: 2000, msg: "查询成功", data: result });
        }
    } catch (err) {
        console.log(err);
        res.send({ code: 5000, msg: "查询失败", data: null });
    }
});

router.get("/:noticeId/:userId",loginMiddleware,async (req,res)=>{
    let noticeId = req.params.noticeId;
    let userId = req.params.userId;
    try{
        let result = await ReadedDao.selectReadedByNoticeIDAndUserID(noticeId,userId);
        console.log(`/Readeds查询的结果/:noticeId=${noticeId}/:userId=${userId}是:\n` +JSON.stringify(result) + "\n————\n");
        res.send({code:2000,msg:"查询成功",data:result});
    }catch(err){
        console.log(err);
        res.send({code:5000,msg:"查询失败",data:null});
    }
})

router.put("/:noticeId/:userId/:ifReaded",loginMiddleware,async (req,res)=>{
    let noticeId = req.params.noticeId;
    let userId = req.params.userId;
    let ifReaded = req.params.ifReaded;
    try {
        let result = await ReadedDao.updateReaded(noticeId, userId, ifReaded);
        console.log(`/Readeds修改的结果/:noticeId=${noticeId}/:userId=${userId}/:ifReaded=${ifReaded}是:\n` +JSON.stringify(result) + "\n————\n");
        res.send({ code: 2000, msg: "修改成功", data: null });
    }
    catch (err) {
        console.log(err);
        res.send({ code: 5000, msg: "修改失败", data: null });
    }

})

module.exports = router;